#!/bin/sh
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#

#
# Run a simple backup of the Bacula build directory then create some           
#   new files, do a differential and restore those two files.
#
TestName="differential-test"
JobName=differential
. scripts/functions

scripts/cleanup
scripts/copy-test-confs
echo "${cwd}/tmp/build" >${cwd}/tmp/file-list
mkdir -p ${cwd}/tmp/build
cp -p ${cwd}/build/src/dird/*.c ${cwd}/tmp/build
cd ${cwd}/tmp
echo "${cwd}/tmp/build/ficheriro1.txt" >restore-list
echo "${cwd}/tmp/build/ficheriro2.txt" >>restore-list
cd ${cwd}

change_jobname CompressedTest $JobName
start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
setdebug level=100 storage=File
label storage=File volume=TestVolume002
label storage=File volume=TestVolume001
run job=$JobName yes
wait
messages
quit
END_OF_DATA

run_bacula  

scripts/check_for_zombie_jobs storage=File
echo "ficheriro1.txt" >${cwd}/tmp/build/ficheriro1.txt
echo "ficheriro2.txt" >${cwd}/tmp/build/ficheriro2.txt

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
@# Force differential on the second Volume
update volume=TestVolume002 VolStatus=Used
run level=differential job=$JobName yes
wait
messages
END_OF_DATA

run_bconsole

scripts/check_for_zombie_jobs storage=File
echo "ficheriro1.txt" >${cwd}/tmp/build/ficheriro1.txt
echo "ficheriro2.txt" >${cwd}/tmp/build/ficheriro2.txt

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
run level=incremental job=$JobName yes
wait
messages
@# 
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
restore where=${cwd}/tmp/bacula-restores storage=File file=<${cwd}/tmp/restore-list
yes
wait
messages
quit
END_OF_DATA

run_bconsole
scripts/check_for_zombie_jobs storage=File
stop_bacula

check_two_logs
#
# Delete .c files because we will only restore the txt files
#
rm -f ${cwd}/tmp/build/*.c
check_restore_tmp_build_diff
end_test
